/**
 * @param {string} s
 * @return {string}
 */
var reverseWords = function (s) {

    let i = 0
    let l = i
    let rsltArr = new Array(s.length)

    for (; i < s.length; i++) {

        if (s[i] === ' ') {
            let tempI = i - 1
            rsltArr[i] = ' '
            while (l < i) {
                rsltArr[tempI--] = s[l++]
            }
            l = i + 1
        }

    }

    if (l !== i) {
        let tempI = i - 1
        rsltArr[tempI] = ' '
        while (l < i) {
            rsltArr[tempI--] = s[l++]
        }
    }
    return rsltArr.join('')
};

console.log(reverseWords("Let's take LeetCode contest"))


//================v1要点总结===============
/**
 * 1.通过找到每一个空格，然后进行分割，倒序写入，感觉有更优雅的办法
 */